﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TMDBAPI;
using IMDBAPI;

namespace Movies
{
    class HelperDefaultMovieSelector
    {
        static String apiKey = HelperSettingProvider.Default().TmdbAPIKey;

        public static MovieDetails GetDefaultMovie(String inputName, String year)
        {

            //TmdbAPI tapi = new TmdbAPI(apiKey);
            ImdbAPI iapi = new ImdbAPI();
            List<String[]> movieResultSummary = new List<String[]>();
            MovieDetails movieDetails = null;
            ImdbMovie imdbM = null;
            TmdbMovie movieBestMatch = null;
            //double lastBestPercentageMatch = 0;
            //double currentPercentageMatch1 = 0;
            //double currentPercentageMatch2 = 0;
            double imdbCurrent = 0;
            try
            {

                if (year != null && year.Length > 0 && !year.Equals("0"))
                {
                    imdbM = iapi.GetMovieByName(inputName, year);
                }
                else
                {
                    imdbM = iapi.GetMovieByName(inputName);
                }
                if (imdbM != null && imdbM.Name != null)
                {
                    imdbCurrent = HelperNameUtil.GetDegreeMatch(HelperNameUtil.NormalizeMovieName(imdbM.Name), inputName);
                    String[] result = new String[3];
                    result[0] = imdbM.Name;
                    result[1] = imdbM.Year;
                    result[2] = imdbM.ImdbID;
                    movieResultSummary.Add(result);
                }

            }
            catch (Exception ex)
            {
                HelperContentProvider.OmitWarning(ex);
            }
            /*
            if (imdbCurrent < 99)
            {

                try
                {
                    TmdbMovie[] movies = tapi.MovieSearch(inputName);
                    List<TmdbMovie> movieBestMatches = new List<TmdbMovie>();

                    foreach (TmdbMovie movie in movies)
                    {
                        String[] result = new String[3];
                        result[0] = movie.Name;
                        result[1] = movie.Year;
                        result[2] = movie.ImdbId;
                        movieResultSummary.Add(result);
                        currentPercentageMatch1 = HelperNameUtil.GetDegreeMatch(HelperNameUtil.NormalizeMovieName(movie.Name), inputName);
                        currentPercentageMatch2 = HelperNameUtil.GetDegreeMatch(HelperNameUtil.NormalizeMovieName(movie.AlternativeName), inputName);
                        if (currentPercentageMatch1 < currentPercentageMatch2)
                        {
                            currentPercentageMatch1 = currentPercentageMatch2;
                        }
                        if (currentPercentageMatch1 > 0 && currentPercentageMatch1 > lastBestPercentageMatch)
                        {
                            movieBestMatch = movie;
                            lastBestPercentageMatch = currentPercentageMatch1;
                        }
                    }

                }
                catch (Exception ex)
                {
                    HelperContentProvider.OmitWarning(ex);
                }


                try
                {
                    if (!(!imdbM.ImdbID.Trim().Equals("") && imdbM.ImdbID.Trim().ToLower().Equals(movieBestMatch.ImdbId.Trim().ToLower())))
                    {
                        imdbM = iapi.GetMovieById(movieBestMatch.ImdbId);
                    }
                }
                catch (Exception ex)
                {
                    HelperContentProvider.OmitWarning(ex);
                }
            }
            

            if (movieBestMatch == null)
            {
                movieBestMatch = new TmdbMovie();
            }
            */
            if (imdbM==null || imdbCurrent == 0)
            {
                return null;
            }

            movieDetails = new MovieDetails(imdbM, movieBestMatch, movieResultSummary);

            return movieDetails;
        }

        public static MovieDetails GetMovieByImdbID(String imdbID)
        {
            ImdbAPI iapi = new ImdbAPI();
            ImdbMovie imdbM = iapi.GetMovieById(imdbID);
            TmdbMovie movieBestMatch = new TmdbMovie();
            MovieDetails movieDetails = new MovieDetails(imdbM, movieBestMatch);
            return movieDetails;
        }

        public static ImdbMovieSearchResults SearchMovieByName(String name, String year)
        {
            ImdbAPI iapi = new ImdbAPI();
            ImdbMovieSearchResults imdbResult = iapi.SearchMovieByName(name, year);
            /*
            ImdbAPI iapi = new ImdbAPI();
            ImdbMovie imdbM = iapi.GetMovieById(imdbID);
            TmdbMovie movieBestMatch = new TmdbMovie();
            MovieDetails movieDetails = new MovieDetails(imdbM, movieBestMatch);
            
            */
            return imdbResult;
        }

    }
}
